
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.schedulebase.dto.DictScheduleApmDto;
import com.rt.schedulebase.entity.DictScheduleApmDuty;
import com.rt.schedulebase.mapper.DictScheduleApmDutyMapper;
import com.rt.schedulenew.api.IDeptDutyService;
import com.rt.schedulenew.api.IDictDeptService;
import com.rt.schedulenew.api.IDictScheduleApmDutyService;
import com.rt.schedulenew.dto.DictScheduleApmDutyDto;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import com.rt.schedulenew.utils.util.DateUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 医生排班表
 */
@Service
public class DictScheduleApmDutyServiceImpl extends ServiceImpl<DictScheduleApmDutyMapper, DictScheduleApmDuty> implements IDictScheduleApmDutyService {

    @Autowired
    private IDeptDutyService deptDutyService;

    @Override
    public boolean insertSelective(DictScheduleApmDuty dictScheduleApmDuty) {
        return retBool(baseMapper.insert(dictScheduleApmDuty));
    }

    @Override
    public boolean deleteByPrimaryKey(String key) {
        return retBool(baseMapper.deleteById(key));
    }

    @Override
    public boolean updateByPrimaryKeySelective(DictScheduleApmDuty dictScheduleApmDuty) {
        return retBool(baseMapper.updateById(dictScheduleApmDuty));
    }

    @Override
    public DictScheduleApmDuty selectByPrimaryKey(String key) {
        return baseMapper.selectById(key);
    }

    @Override
    public List<DictScheduleApmDuty> getList(DictScheduleApmDutyDto dto) {
        QueryWrapper<DictScheduleApmDuty> qw = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<String, Object>();
        qw.allEq(map, false);
        List<DictScheduleApmDuty> list = baseMapper.selectList(qw);
        return list;
    }

    @Override
    public List<DictScheduleApmDuty> getListAll() {
        return baseMapper.selectList(null);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public JsonResult copyDuty(DictScheduleApmDutyDto dto) {
        List<DictScheduleApmDuty> lsDuty = baseMapper.getDutyList(dto);
        int intervalDays = DateUtil.getIntervalDays(dto.getStartDate(), dto.getDutyDate());

        DictScheduleApmDutyDto dutyDto = new DictScheduleApmDutyDto();
        try {
            BeanUtils.copyProperties(dutyDto, dto);
            dutyDto.setStartDate(dto.getDutyDate());
            dutyDto.setEndDate(DateUtil.addDays2(dto.getDutyDate(),intervalDays));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return JsonResultUtil.failure(e.getMessage());
        }
        List<DictScheduleApmDto> apmList = deptDutyService.getEnableApmList(dutyDto);
        List<String> apmCodes = apmList.stream().map(x -> x.getApmCode()).collect(Collectors.toList());
        for (DictScheduleApmDuty duty : lsDuty) {
            if(apmCodes.contains(duty.getApmCode())){
                String dateStr = DateUtil.addDays2(duty.getScheduleDate(), intervalDays);
                duty.setScheduleDate(dateStr);
                insertSelective(duty);
            }
        }
        return JsonResultUtil.success();
    }

    @Override
    public List<DictScheduleApmDuty> getDutyList(DictScheduleApmDutyDto dto) {
        return baseMapper.getDutyList(dto);
    }

    @Override
    public boolean delete(DictScheduleApmDutyDto dto) {
        return retBool(baseMapper.delete(dto));
    }
}
